<template>
  <el-dialog
    :visible.sync="visible"
    :close-on-click-modal="false"
    append-to-body
    :before-close="beforeClose"
    top="30px"
    width="600px"
  >
    <el-form ref="form" :model="editData" :rules="rules" label-width="100px">
      <el-form-item label="类型值" prop="enum_key">
        <el-input
          v-model="editData.enum_key"
          class="form-control-ml"
          :disabled="tableId"
        ></el-input>
      </el-form-item>
      <el-form-item label="类型名称" prop="enum_value">
        <el-input
          v-model="editData.enum_value"
          class="form-control-ml"
          placeholder="请填写类型名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn-long" type="primary" @click="onSubmit" :loading="submitLoading">
          {{ tableId ? '修改' : '添加' }}
        </el-button>
        <el-button class="btn-long" @click="beforeClose" :loading="submitLoading">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { add } from '@vimi/utils-tool'
export default {
  components: {},
  props: {
    visible: Boolean,
    formData: { type: Object, default: () => ({}) },
    maxKey: String,
  },
  data() {
    return {
      editData: {
        enum_value: '',
        enum_key: '',
      },
      rules: {
        enum_key: { required: true, message: '请输入类型值', trigger: 'blur' },
        enum_value: { required: true, message: '请输入类型名称', trigger: 'blur' },
      },
      enumField: 'credit_agency_info.agency_type_sub',
      submitLoading: false,
      tableName: 'system_enum',
    }
  },
  computed: {
    tableId() {
      return !!this.formData.enum_field
    },
  },
  watch: {
    formData: {
      handler: function () {
        this.editData.enum_value = this.formData.enum_value
        this.editData.enum_key = this.formData.enum_key || add(this.maxKey, 1)
      },
      immediate: true,
      deep: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    upadateData() {
      this.submitLoading = true
      this.$api
        .updateDataBody(
          'credit',
          this.tableName,
          {},
          {
            apiKey: 'enum_update',
            enum_field: this.enumField,
            ...this.editData,
          }
        )
        .then(() => {
          this.$message.success('修改成功')
          this.$emit('success')
          this.beforeClose()
        })
        .catch(err => {
          this.$message.error(err?.data?.error_description || '提交失败，请重试！')
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
    addData() {
      this.submitLoading = true
      this.$api
        .addData('credit', this.tableName, { ...this.editData, enum_field: this.enumField })
        .then(() => {
          this.$message.success('添加成功')
          this.$emit('success')
          this.beforeClose()
        })
        .catch(err => {
          this.$message.error(err?.data?.error_description || '提交失败，请重试！')
        })
        .finally(() => {
          this.submitLoading = false
        })
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.tableId) {
            this.upadateData()
          } else {
            this.editData.order_num = this.editData.enum_key
            this.addData()
          }
        } else {
          this.$message.error('输入错误，请重新填写!')
        }
      })
    },
    beforeClose() {
      this.$emit('update:visible', false)
    },
  },
}
</script>

<style lang="scss" scoped></style>
